/*
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "imageknife_test.h"
#include "imageknife.h"
#include "napi_parser.h"
#include "promise_napi.h"
namespace ImageKnifeTest {
using namespace ImageKnifePro;
 
struct TestData {
    std::string info = "Test Custom Data In Loading Callback";
    int num = 0;
};

static napi_value CustomDataOnFinalize(napi_env env, napi_callback_info info)
{
    size_t argc = 1;
    napi_value value;
    napi_get_cb_info(env, info, &argc, &value, nullptr, nullptr);

    TestData *data = new TestData();
    data->num = ImageKnifePro::NapiParser::ParseInt(env, value);

    auto promise = std::make_shared<ImageKnifePro::PromiseNapi<int>>();
    auto option = std::make_shared<ImageKnifePro::ImageKnifeOption>();
    option->customData = data;
    option->onFinalize = [promise](void *customData) {
        TestData *data = static_cast<TestData *>(customData);
        IMAGE_KNIFE_LOG(LOG_INFO, "Custom Data: %{public}d On Finalize", data->num);
        promise->Resolve(data->num);
        delete data;
    };
    return promise->GetPromise();
}

static napi_value CustomDataAccessByOnloadStart(napi_env env, napi_callback_info info)
{
    const size_t count = 2;
    size_t argc = count;
    napi_value args[count] = {nullptr};
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);

    TestData *data = new TestData();
    data->num = ImageKnifePro::NapiParser::ParseInt(env, args[1]);

    auto option = std::make_shared<ImageKnifePro::ImageKnifeOption>();
    option->loadSrc.SetString(ImageKnifePro::NapiParser::ParseString(env, args[0]));
    option->customData = data;
    option->onFinalize = [](void *customData) {
        TestData *data = static_cast<TestData *>(customData);
        delete data;
    };

    // 设置回调
    auto onLoadListener = std::make_shared<ImageKnifePro::OnLoadCallBack>();
    option->onLoadListener = onLoadListener;

    auto promise = std::make_shared<ImageKnifePro::PromiseNapi<int>>();
    onLoadListener->onLoadStart = [promise](ImageKnifePro::ImageInfo imageInfo) {
        TestData *data = static_cast<TestData *>(imageInfo.customData);
        IMAGE_KNIFE_LOG(LOG_INFO, "%{public}s : onLoadStart", data->info.c_str());
        promise->Resolve(data->num);
    };

    ImageKnifePro::ImageKnife::GetInstance().Preload(option);

    return promise->GetPromise();
}

napi_value CustomDataSetupTests(napi_env env, napi_value &exports)
{
    napi_property_descriptor desc[] = {
        {"customDataAccessByOnloadStart", nullptr, CustomDataAccessByOnloadStart,
            nullptr, nullptr, nullptr, napi_default, nullptr},
        {"customDataOnFinalize", nullptr, CustomDataOnFinalize, nullptr, nullptr, nullptr, napi_default, nullptr},
    };
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}

}

